var Connect = YAHOO.util.Connect,
Panel = YAHOO.widget.Panel,
KeyListener = YAHOO.util.KeyListener,
Event = YAHOO.util.Event,
Dom = YAHOO.util.Dom,
Json = YAHOO.lang.JSON;
var currentPage = 1;
var countItem = 1;
var MyController = {

    asyncRequestFalse:function(){
        alert('AsyncRequest False');
    },
    
    addProductToShopCat:function(productId, categoryId){
        var strPost = 'productId='+productId;
        strPost+='&categoryId='+categoryId;
        var strUrl = '/saveProductToShopCat/?ajax=1';
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.addToCatSuccess,
            failure: MyController.asyncRequestFalse
        },strPost);
    },

    addToCatSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        Dom.get('product_order_'+aryData.productId).innerHTML = aryData.strContent;
        if(Dom.get('count_shopping_cart')){
            Dom.get('count_shopping_cart').innerHTML = aryData.totalProduct;
        }
        
    },
    
    innerError:function(aryErrorData){

        for (var property in aryErrorData) {
            if (aryErrorData.hasOwnProperty(property)) {
                try{
                    document.getElementById(property).style.display = 'inline';
                    document.getElementById(property).innerHTML = aryErrorData[property];
                }catch(e){
                    alert(e.message);
                }
            }
        }
    },
    
    clearError:function(){
        var elementError = Dom.getElementsByClassName('register_error');
        var errorLenght = elementError.length;
        for(var i = 0; i < errorLenght; i++){
            elementError[i].style.display = 'none';
        }
    },
    showOverlaySuccess:function(messageSuccess){
        Dom.get('confirm-header').innerHTML = 'Thành công';
        Dom.get('confirm-message').innerHTML = messageSuccess;
        MyController.showOverlayConfirm();
    },
    showOverlayError:function(messageError){
        Dom.get('confirm-header').innerHTML = 'Có lỗi xảy ra';
        Dom.get('confirm-message').innerHTML = messageError;
        MyController.showOverlayConfirm();
    },
    
    showOverlayConfirm:function(){
//        var x = 450;
//        var y = 100;
        MyController.overlayConfirm = new YAHOO.widget.Panel('overlayConfirm',
        {
            //            xy:[x,y],
            fixedcenter:true,
            close:false,
            visible:false,
            draggable:false,
            modal:true,
            width:"400px",
            zIndex: 255
        });

        // Render the Dialogs
        MyController.overlayConfirm.render(document.body);
        MyController.overlayConfirm.center();
        document.getElementById('overlayConfirm').style.display = 'block';
        MyController.overlayConfirm.show();
    },

    showOverlayCommon:function(message){
        Dom.get('confirm-header-common').innerHTML = message;
        MyController.overlayCommon();
    },

    overlayCommon:function(){
        var x = 450;
        var y = 100;
        MyController.overlayContent = new YAHOO.widget.Panel('overlayCommon',
        {
            //            xy:[x,y],
            fixedcenter:true,
            close:false,
            visible:false,
            draggable:false,
            modal:true,
            width:"500px",
            zIndex: 255
        });

        // Render the Dialogs
        MyController.overlayContent.render(document.body);
        MyController.overlayContent.center();
        document.getElementById('overlayCommon').style.display = 'block';
        MyController.overlayContent.show();
    },
    

    closeOverlayConfirm:function(){
        MyController.overlayConfirm.hide();
    },

    closeOverlayCommon:function(){
        MyController.overlayContent.hide();
    },

    backOrder:function(){
        history.go(-1);
    },
    reloadOrder:function(){
        window.location.reload();
    },
    
    changeCaptcha:function(){
        var Stamp = new Date();
        var second = Stamp.getSeconds()/1000;
        document.getElementById('verify_image').src = "../../lib/captcha.php?ran="+second;
    },

    customerRigester:function(){
        MyController.clearError();
        var strUrl = '/registerCustomer/?ajax=1';
        YAHOO.util.Connect.setForm('UserRegisterForm');
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.customerRigesterSuccess,
            failure: MyController.asyncRequestFalse
        });
        YAHOO.util.Connect.resetFormState();
    },

    customerRigesterSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        if(aryData.intOk == 1){
            MyController.showOverlaySuccess(aryData.message);
//            window.location.href = '/maccathue/login/';
        }else{
            MyController.showOverlayError(aryData.messageError);
            MyController.changeCaptcha();
            Dom.get('verify_image').value = '';
            //            MaccathueController.backToTop();
            MyController.innerError(aryData.aryError);
        }
    }
}


